package com.gdth.widget.tabactivity;

import java.util.ArrayList;
import java.util.List;

import com.gdth.R;
import com.gdth.utils.DensityUtil;
import com.gdth.widget.viewpager.AutoViewPager;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

public class BaseTabActivity extends LinearLayout{

	private int currIndex = 0;
	private AutoViewPager mAutoViewPager;
	private List<Drawable> listDrawable, listDrawableH;
	
	private List<TabTextView> listTextView;
	
	private int defaultColor = R.color.snow;
	private int hightColor = R.color.black;
	
	private int tabHeight;
	
	public BaseTabActivity(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	public BaseTabActivity(Context context, List<View> listView, List<Drawable> listDrawable, List<Drawable> listDrawableH, List<String> listVal) {
		super(context);
		// TODO Auto-generated constructor stub
		
		tabHeight = DensityUtil.dip2px(context, 50);
		
		this.listDrawable = listDrawable;
		this.listDrawableH = listDrawableH;
		this.setOrientation(LinearLayout.VERTICAL);
		
		LayoutParams nnLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		this.setLayoutParams(nnLayoutParams);
		
		LayoutParams mLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1);
		LayoutParams tLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, tabHeight, 0);
		LayoutParams hLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, 1, 0);
		LayoutParams txtLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, 1);
		
		mAutoViewPager = new AutoViewPager(context, listView);
		mAutoViewPager.setLayoutParams(mLayoutParams);
		mAutoViewPager.addOnPageChangeListener(new MyOnPageChangeListener());
		mAutoViewPager.setCurrentItem(currIndex);
		
		
		LinearLayout tabLayout = new LinearLayout(context);
		
		tabLayout.setOrientation(LinearLayout.HORIZONTAL);
		tabLayout.setLayoutParams(tLayoutParams);
		tabLayout.setGravity(Gravity.CENTER_VERTICAL);
		
		tabLayout.setBackgroundColor(context.getResources().getColor(R.color.dimgray));

		listTextView = new ArrayList<TabTextView>();
		
		int count = listView.size();
		for (int i = 0; i < count; i++){
			TabTextView mTabTextView = new TabTextView(context);
			mTabTextView.setText(listVal.get(i));
			mTabTextView.setGravity(Gravity.CENTER);
			mTabTextView.setImage(this.listDrawable.get(i), this.defaultColor);
			mTabTextView.setLayoutParams(txtLayoutParams);
			mTabTextView.setOnClickListener(new MyOnClickListener(i));
			tabLayout.addView(mTabTextView);
			
			listTextView.add(mTabTextView);
		}
		
		View hView = new View(context);
		hView.setLayoutParams(hLayoutParams);
		hView.setBackgroundColor(context.getResources().getColor(R.color.black));
		this.addView(mAutoViewPager);
		this.addView(hView);
		this.addView(tabLayout);
	}
	
	
	/**
     * 滑动控件点击事件 
     */
    private class MyOnClickListener implements OnClickListener{
    	private int index=0;
    	
    	//构造函数用于传参
    	public MyOnClickListener(int i){
    		index=i;
    	}
    	
    	public void onClick(View v) {
    		mAutoViewPager.setCurrentItem(index); 
        }
    }
	
	 public class MyOnPageChangeListener implements OnPageChangeListener{
        public void onPageScrollStateChanged(int arg0) {
        }

        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        public void onPageSelected(int arg0) {
        	PageChange(arg0);
        }
    }
    
    /**
     * ViewPager切换效果
     * @param arg0
     */
	private void PageChange(int arg0){
		if (arg0 == currIndex) return;
		
		listTextView.get(currIndex).setImage(this.listDrawable.get(currIndex), this.defaultColor);
		listTextView.get(arg0).setImage(this.listDrawableH.get(arg0), this.hightColor);
    	
    	currIndex = arg0;
    }
}
